package io.gitee.yth.design.pattern.test.core.pattern.behavioral.command;

import java.util.ArrayList;
import java.util.List;

/**
 * MacroCommand 管理一个子命令序列
 *
 * @author yth
 * @since 2023-04-29-1:00
 */
public class MacroCommand implements Command {

    private final List<Command> commands = new ArrayList<>();

    public void add(Command command) {
        commands.add(command);
    }

    public void remove(Command command) {
        commands.remove(command);
    }

    @Override
    public void execute() {
        for (Command command : commands) {
            command.execute();
        }
    }

    @Override
    public void undo() {
        /*
        注意撤销要按照反方向来操作
         */
        for (int i = commands.size() - 1; i >= 0; i--) {
            commands.get(i).undo();
        }
    }
}
